package cn.com.text.Activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import cn.com.text.Dao.User;
import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import cn.com.text.MainActivity;
import cn.com.text.R;
import cn.com.text.Util.ImageHelper;
import cn.com.text.Util.MySQLConnections;

public class LogActivity extends AppCompatActivity {
    private Button button_submit;
    private EditText username;
    private EditText password;
    private Button button_reg;
    private ImageView imageView;
    private final int CODE_PICK_PHOTO = 1;
    private Uri imageUri;
    private String bitmapToString = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        LitePal.getDatabase();

        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        imageView = (ImageView) findViewById(R.id.userImage);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_gallery = new Intent(Intent.ACTION_PICK);
                intent_gallery.setType("image/*");
                startActivityForResult(intent_gallery, CODE_PICK_PHOTO);
            }
        });
        button_submit = (Button) findViewById(R.id.button_submit);
        button_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((!TextUtils.isEmpty(username.getText()) && (!TextUtils.isEmpty(password.getText())))) {
                    User usr = new User();
                    usr.setName(username.getText().toString());
                    usr.setPassword(password.getText().toString());
                    usr.setImage(bitmapToString);
                    //检查是否有此用户
                    List<User> userList = DataSupport.where("name = ? and password = ?", usr.getName(),usr.getPassword()).find(User.class);
                    if (userList.isEmpty()) {
                        Toast.makeText(LogActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    } else {
                        Log.d("username", username.getText().toString());
                        Toast.makeText(LogActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LogActivity.this, MainActivity.class);
                        intent.putExtra("username",usr.getName());
                        intent.putExtra("password",usr.getPassword());
                       // intent.putExtra("image",bitmapToString);
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                }else {
                    Toast.makeText(LogActivity.this,"用户名或密码不为空",Toast.LENGTH_SHORT).show();
                }
            }
        });
        button_reg = (Button) findViewById(R.id.reg_button);
        button_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ((!TextUtils.isEmpty(username.getText()) && (!TextUtils.isEmpty(password.getText())))) {
                    final User usr = new User();
                    final String usrname = username.getText().toString();
                    final String usrpassword = password.getText().toString();
                    usr.setName(usrname);
                    usr.setPassword(usrpassword);
                    usr.setImage(bitmapToString);
                    //检查是否有此用户
                    List<User> userList = DataSupport.where("name = ?", usr.getName()).find(User.class);
                    if(bitmapToString != null){
                        if (userList.isEmpty()) {
                            Thread thread = null;
                            thread = new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        MySQLConnections.insertIntoUser(usrname,usrpassword,bitmapToString);
                                    } catch (SQLException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            thread.start();
                            try {
                                thread.join();
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            usr.save();
                            Toast.makeText(LogActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(LogActivity.this, "重复注册，请登录", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(LogActivity.this,"请选择头像",Toast.LENGTH_SHORT).show();
                    }

                }else{
                    Toast.makeText(LogActivity.this,"注册用户名或密码不为空",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CODE_PICK_PHOTO) {//相册
            imageUri= data.getData();
            //获取照片路径
            imageView.setImageURI(imageUri);
            Bitmap bitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                bitmapToString = ImageHelper.bitmapToString(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
